package com.lagou.edu.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 属性工具
 * 
 * @author ws 2020年5月12日 上午4:33:55
 * @version 1.0
 */
public class PropertiesUtil {
    
    private static Log logger = LogFactory.getLog(PropertiesUtil.class);
    
    /**
     * the map sotre the file name and properties data
     */
    private static ConcurrentHashMap<String, Properties> mapFileProperties = new ConcurrentHashMap<String, Properties>();

    public static String getPropertyValue(String fileName, String propertyName)
    {
        Properties properties = mapFileProperties.get(fileName);
        if (properties==null)
        {
            properties = initialize(fileName);
            mapFileProperties.put(fileName, properties);
        }
        
        if(!(properties==null))
        {
            return properties.getProperty(propertyName);
        }
        return null;
    }

    /**
     * init the properties file 
     *
     * @param fileName
     * @return
     */
    private static Properties initialize(String fileName)
    {
        Properties prop = new Properties();
        try
        {
            InputStreamReader in = new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "UTF-8");
            prop.load(in);
            return prop;
        }
        catch (FileNotFoundException e)
        {
            logger.error("The properties file not found! file name => " + fileName, e);
        }
        catch (IOException e)
        {
            logger.error("The properties file style is not right! file name => " + fileName, e);
        }
        
        return null;
    }
}
